<?php 
	include($_SERVER['DOCUMENT_ROOT']."/headerins.php");
?>

<?php
			
			$b = 0;
			$b = getParent(0,'Nurpiisov', 0);
			//setBallsToUser(13,1000,$b);
	
			//echo "\n\nsalary --------\n\n";
			setSalary(1);

			printTree(0);
		
		function printTree($number){
			//print name
			$sql = "select id, Number, ParentNumber, LastName, Name, Salary, Balls, GroupBalls, Percents from users where Number = ".$number;

			$result = mysql_query($sql);
			$rows = mysql_num_rows($result);
			if($rows > 0){
				$row = mysql_fetch_array($result);
				echo $number.' - Number: '.$row['Number'].'; Sub:'.$row['ParentNumber'].'; LN: '.$row['LastName'].'; Salary: '.$row['Salary'].'<br>'; 
			}
			
			//print sub peolpe
			$sql = "select id, Number, ParentNumber, LastName, Name, Salary, Balls, GroupBalls, Percents from users where ParentNumber = ".$number;
			$result = mysql_query($sql);

			$rows = mysql_num_rows($result);
			if($rows > 0){
				while($row = mysql_fetch_array($result)){
					printTree($row['Number']);
				}
			}
		}	
		
		function setSalary($number){

		$sql = "select id, number, Balls, GroupBalls, Percents from users where Number = ".$number;
		$result = mysql_query($sql);
		$rows = mysql_num_rows($result);
		if ($rows > 0 ) {

			while($row = mysql_fetch_array($result)){
				$Balls = $row['Balls'] + $row['GroupBalls'];
				$Percents = $row['Percents'];
				if($Balls == null) $Balls = 0;
				if($Percents == null) $Percents = 0;
					
				$Salary = 0;
				$Salary = $Balls * $Percents * 0.57;

				setSalaryToUser($row['id'], $Salary);
				//echo  "number: ".$row['number'].", Salary: ".$Salary."\n";

				$sql2 = "select id, number, Balls, GroupBalls, Percents from users where ParentNumber = ".$row['number'];
				$result2 = mysql_query($sql2);
						$rows2 = mysql_num_rows($result2);
						if($rows2 > 0){
							$row2 = mysql_fetch_array($result2);
							
							$Balls2 = $row2['Balls'] + $row2['GroupBalls'];
							$Percents2 = $row2['Percents'];
							if($Balls2 == null) $Balls2 = 0;
							if($Percents2 == null) $Percents2 = 0;

							$Salary2 = 0;
							$Diff = $Percents - $Percents2;
							if($Diff <= 0)
								$Salary2 = 0;
							else
								$Salary2 = $Balls2 * $Diff * 0.57;

							setSalaryToUser($row2['id'], $Salary2);
							//echo  "number: ".$row2['number'].", Salary: ".$Salary2."\n";

							//setSalary($row2['number']);
						}
			}
		}
		}

	    //mysql_close($con);
        $i = 0;
		function getParent($number, $name, $i){
			$sql = "select id, number, lastname from users where parentNumber = ".$number;
			$result = mysql_query($sql);
            $rows = mysql_num_rows($result);
            //print("aaa ".$rows);
            if ($rows > 0 ) {
                while($row = mysql_fetch_array($result)){
                    $i=$i+1;

					$sql2 = "select sum(l.ball * l.Qty) balls
								from `order` o
								left join orderline l on l.OrderID = o.ID
								where o.userIDTo = ".$row['id'];
					$result2 = mysql_query($sql2);
					$rows2 = mysql_num_rows($result2);
					$Balls = 0;
					
					//echo $rows2."\n";

					if($rows2 > 0){
						$row2 = mysql_fetch_array($result2);
						$Balls = $row2['balls'];
					}else
						$Balls = 0;
					//$Percents = $balls
                    
					/*print('<img id="menu'.$i.'-img" style="width:11px;length:11px" src="Pictures/+.gif" onclick="javascript:setmenu('."'".'menu'.$i.''."'".');" /> ');
                    print("<div id='node'>".$row['lastname']."<br>1000</div>");
                    print('<div id="menu'.$i.'" style="display:none">');
                    print('<ul>');*/
					$GroupBalls = 0;
                    $GroupBalls = $GroupBalls + getParent($row['number'],$row['lastname'],$i);
					$Percents = getPercent($Balls + $GroupBalls);
					//echo $i.": "	.$name	."->".	$row['lastname'].	", Balls:"	.$Balls	.", %: ".$Percents.", GroupBalls: ".$GroupBalls.	"\n";
                    $GroupBalls = $GroupBalls + $Balls;
					setBallsToUser($row['id'], $Balls, $GroupBalls, $Percents);
					return $GroupBalls;
					/*print('</ul>');
                    print('</div>');*/
                }
            }
		}

function setBallsToUser($userID, $Balls, $GroupBalls, $Percents){
					if($Balls == null)
						$Ball = 0;
					if($GroupBalls == null)
						$GroupBalls = 0;
					if($Percents == null)
						$Percents = 0;
					if($userID != null and $userID > 0){
						$sql = "update users set balls = ".$Balls.", groupballs = ".$GroupBalls.", percents = ".$Percents." 	where ID = ".$userID;
						//echo $sql."\n";
						mysql_query($sql);
					}
}

function setSalaryToUser($userID, $Salary){
					if($Salary == null)
						$Salary = 0;
					if($userID != null and $userID > 0){
						$sql = "update users set Salary = ".$Salary." where ID = ".$userID;
						//echo $sql."\n";
						mysql_query($sql);
					}
}


function getPercent($balls){
	if($balls < 200 )
		return 0;
	elseif($balls >= 200 and $balls < 600)
		return 3;
	elseif($balls >= 600 and $balls < 1200)
		return 6;
	elseif($balls >= 1200 and $balls < 2400)
		return 9;
	elseif($balls >= 2400 and $balls < 4000)
		return 12;
	elseif($balls >= 4000 and $balls < 7000)
		return 15;
	elseif($balls >= 7000 and $balls < 10000)
		return 8;
	elseif($balls >= 10000)
		return 21;
}

?>